import router from './router'
import store from './store'
// import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import getPageTitle from '@/utils/get-page-title'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

router.beforeEach(async(to, from, next) => {
/*  // start progress bar
  NProgress.start()

  // set page title
  document.title = getPageTitle(to.meta.title)

  next()*/
  if (to.matched.some(record => record.meta.requireAuth)) { // 检查是否需要登录权限
      if (store.state.user.token) { // 未登录，但是有token，获取用户信息
        try {
          store.dispatch("user/getUserInfo" , store.state.user).then(res=>{
            if (res.code === 0) {
              next()
            } else {
              //window.alert('请登录')
              store.commit('user/resetToken')
              next({ name: 'Login' })
            }
          }).catch(err =>{
            console.log("err" , err)
          })
        } catch (err) {
          //window.alert('请登录')
          store.commit('user/resetToken')
          next({ name: 'Login' })
        }
      } else {
        //window.alert('请登录')
        next({ name: 'Login' })
      }
    } else {
      next()
    }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
